var gulp = require("gulp"),
webserver = require("gulp-webserver"),
livereload = require("gulp-livereload"),
sass = require("gulp-ruby-sass"),
uglify = require("gulp-uglify"),
rename = require("gulp-rename"),
sourcemap = require("gulp-sourcemaps"),
tinypng = require("gulp-tinypng");


//自动刷新网站
gulp.task("webserver",function(){
	gulp.src("dest")
	.pipe(webserver({
		livereload:true,
		open:true
	}))
})

//2。把html文件从开发环境发布到发布环境
gulp.task("html",function(){
	return gulp.src("src/**/*.html")
	.pipe(gulp.dest("dest"))
})

//4.编译sass文件
gulp.task("sass",function(){
	return sass("src/sass/*.scss",{style:"compressed"})
	.on("error",function(err){
		console.log(err);
	})
	.pipe(gulp.dest("dest/css"))
})
// 5.js文件压缩gulp-uglify&重命名gulp-rename
gulp.task("js",function(){
    return gulp.src("src/js/*.js")//读取js文件
    .pipe(sourcemap.init())//在js文件刚读入时，将js文件初始化到地图中
    .pipe(uglify())//压缩
    .pipe(rename({suffix:".min"}))//重名名添加后缀.min
    .pipe(sourcemap.write("map"))//初始化到的js数据写入到map
    .pipe(gulp.dest("dest/js"));//输出js文件

})
//6.图片
gulp.task("tinypng",function(){
	return gulp.src("src/images/**/*.{png,jpg,gif,svg}")
	// .pipe(tinypng("填写TinyPN API KEY"))
	.pipe(gulp.dest("dest/images"));
})
//3.监听任务
gulp.task("watch",function(){
   gulp.watch("src/**/*.html",["html"]);//监听该路径下的html文件，如果发生改变执行html代码
   gulp.watch("src/sass/*.scss",["sass"]);
   gulp.watch("src/js/*.js",["js"]);
   gulp.watch("src/images/**.*.{png,jpg,gif,svg}",["images"])

})

gulp.task("default",["webserver","html","sass","js","tinypng","watch"]);